<?php
/**
 * Created by DRCOM.
 * User: GZX
 * Datetime: 2022-01-19 16:39
 */

header("Access-Control-Allow-Origin: *");
header('Content-Type: text/html;charset=utf-8');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');
header("Access-Control-Allow-Headers: *");

$img = $_POST['img'];
$file = $_FILES['file'];//得到传输的数据

//得到文件名称
$name = $file['name'];
$type = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件类型，并且都转化成小写
$allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型
//判断文件类型是否被允许上传
if(!in_array($type, $allow_type)){
    //如果不被允许，则直接停止程序运行
    return ;
}
//判断是否是通过HTTP POST上传的
if(!is_uploaded_file($file['tmp_name'])){
    //如果不是通过HTTP POST上传的
    return ;
}
$upload_path = "./img/$img/"; //上传文件的存放路径
//开始移动文件到相应的文件夹
if(move_uploaded_file($file['tmp_name'],$upload_path.$file['name'])){
    echo trim($upload_path.$file['name'],'.');
}else{
    echo "Failed!";
}